﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Media;

namespace BibliothequeMedia
{

    public class MyFileInfo
    {
        public string Titre { get; set; }
        public string Auteur { get; set; }
        public string Piste { get; set; }
        public string Durée { get; set; }
    }
    /// <summary>
    /// Interaction logic for MainWindow.xamls
    /// </summary>
    public partial class Bibliotheque : Window
    {
        public Bibliotheque()
        {
            InitializeComponent();
        }

        private void checkAndSetInfo(AMediaInfo MediaDrop)
        {
            if (MediaDrop.getTitle() == null)
                MediaDrop.setTitle("Aucune référence");
            if (MediaDrop.getAuthor() == null)
                MediaDrop.setAuthor("Aucune référence");
            if (MediaDrop.getTrackNumber() == null)
                MediaDrop.setTrackNumber("?");
            if (MediaDrop.getLength() == null)
                MediaDrop.setLength("...");
        }
        #region Creation de la List de Musique Info
        private List<MyFileInfo> LoadCollectionMedia(DragEventArgs e, string[] droppedFilePaths)
        {
            List<MyFileInfo> MediaInfo = new List<MyFileInfo>();

            foreach (string droppedFilePath in droppedFilePaths)
            {
                AMediaInfo MediaDrop = Media.MediaInfoFactory.getInfoOfMedia(droppedFilePath);
                if (MediaDrop is MusicInfo)
                {
                    checkAndSetInfo(MediaDrop);
                    MediaInfo.Add(new MyFileInfo() { Titre = MediaDrop.getTitle(), Auteur = MediaDrop.getAuthor(), Piste = MediaDrop.getTrackNumber(), Durée = MediaDrop.getLength()});
                }
                else if (MediaDrop is MovieInfo)
                {
                    checkAndSetInfo(MediaDrop);
                    MediaInfo.Add(new MyFileInfo() { Titre = MediaDrop.getTitle(), Auteur = MediaDrop.getAuthor(), Piste = MediaDrop.getTrackNumber(), Durée = MediaDrop.getLength() });
                }
            }
            return MediaInfo;
        }
        #endregion

        #region Gestion du drag and drop
        private void FilesDropped(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] droppedFilePaths = e.Data.GetData(DataFormats.FileDrop, true) as string[];
                MediaGrid.ItemsSource = LoadCollectionMedia(e, droppedFilePaths);
            }
        }
        #endregion
    }
}
